import {init} from '../../../utils/realtime';
import {getObject, getPointer, getQuery} from '../../../utils/Av';
export async function createPushRoom (toId) {
  var room = null;
  var realtime = init ();
  var user = AV.User.current ();
  return realtime
    .createIMClient (user.toJSON ().objectId)
    .then (function (tom) {
      // 创建与Jerry之间的对话
      room = tom.createConversation ({
        members: [toId],
        unique: true,
        name: 'add friend',
      });
      return room;
    });
}
export async function post_push_user (toId) {
  var user = AV.User.current ();
  var friendQ1 = getQuery ('friend').equalTo (
    'from',
    getPointer ('_User', user.toJSON ().objectId)
  );
  var friendQ2 = getQuery ('friend').equalTo ('to', getPointer ('_User', toId));

  var frendsListOne = await AV.Query.and (friendQ1, friendQ2).first ();
  var resultOne = {};
  if (!frendsListOne) {
    var friendObject = getObject ('friend');
    friendObject.set ('from', getPointer ('_User', user.toJSON ().objectId));
    friendObject.set ('to', getPointer ('_User', toId));
    resultOne = await friendObject.save ().catch (console.error);

    var friendObject1 = getObject ('friend');
    friendObject1.set ('from', getPointer ('_User', toId));
    friendObject1.set ('to', getPointer ('_User', user.toJSON ().objectId));
    resultOne = await friendObject1.save ().catch (console.error);
    return resultOne;
  } else {
    return frendsListOne;
  }
}
